package com.project.forlearning.controller;

import com.alibaba.fastjson.JSON;
import com.project.forlearning.pojo.vo.ResponseInfo;
import com.project.forlearning.service.BackLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

/**
 * 管理员登录API
 */
@Api(value = "BackLoginController", tags={"后台登录页面相关API"})
@RestController
@RequestMapping("/backLogin")
@CrossOrigin
public class BackLoginController {

    @Resource
    private BackLoginService backLoginServiceImpl;

    /**
     * 管理员注册
     */
    @ApiOperation("管理员注册：需要输入邮箱和密码，通过邮箱发送账号，管理员获取账号进行登录")
    @PostMapping("/register")
    public String registerAdmin(@ApiParam(value = "邮箱号", required = true) @RequestParam(value = "adminMail") String adminMail,
                                @ApiParam(value = "密码", required = true) @RequestParam(value = "adminPass") String adminPass
                                ) throws MessagingException {
        return backLoginServiceImpl.registerAdmin(adminMail, adminPass);
    }

    /**
     * 无权限访问
     */
    @ApiOperation("无权限：如果用户无权限则通过以下返回数据")
    @GetMapping("/uauth")
    public String adminUAuth(){
        return JSON.toJSONString(new ResponseInfo<>(401, "用户无访问权限", false));
    }

    @ApiOperation("管理员登录接口")
    @PostMapping("/login")
    public String loginAdmin(@ApiParam(value = "管理员账号", required = true) @RequestParam(value = "adminName") String adminName,
                             @ApiParam(value = "管理员密码", required = true) @RequestParam(value = "adminPass") String adminPass,
                             HttpServletRequest request){
        return backLoginServiceImpl.loginAdmin(adminName, adminPass, request);
    }

    /**
     * 忘记密码
     */
    @ApiOperation("忘记密码api")
    @PostMapping("/wangJiMiMa")
    public String wangJiMiMa(@ApiParam(value = "邮箱号", required = true)@RequestParam(value = "adminMail") String adminMail,
                             @ApiParam(value = "新密码", required = true) @RequestParam(value = "adminPass") String adminPass){
        return backLoginServiceImpl.updateAdminPass(adminMail, adminPass);
    }

    /**
     * 注销
     */
    @ApiOperation("注销（退出）")
    @GetMapping( "/logout")
    public String logout() {
        return backLoginServiceImpl.logout();
    }

}
